package cn.xiaoqintools.studypark.utils;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.widget.TextView;



import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.xiaoqintools.studypark.R;
import cn.xiaoqintools.studypark.base.AppContext;

/**
 * Created by zzd on 2015/11/18.
 */
public class StringUtils {
    /**
     * please use a format like this:
     *      这是%1$s个Demo字%2$s符串，你要%3$s使用%4$s个字符
     *
     * @author zzd
     *
     * created at 2015/11/19 22:19
     */
    public static SpannableString getSpannableString(Context context, int resourceId, String... span) {
        String resource = context.getString(resourceId);
        SpannableString msp = new SpannableString(context.getString(resourceId, span));
        //获取%s的位置
        int index = resource.indexOf("%");
        int lastIndex = resource.lastIndexOf("%");
        int part = index;
        msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_red)), part,
                part + span[0].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int i = 1;
        while(true){
            if(index == lastIndex){
                break;
            }
            index = resource.indexOf("%", index+1);
            int delta = 0;
            for(int j = 0; j<i;j++) {
                delta += span[j].length();
            }
            part = index - 4 * (i) + delta;
            msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_red)), part,
                    part + span[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            i++;
        }

        return msp;
    }


    /**
     * 加中划线（删除线）效果
     *
     * @author zzd
     *
     * created at 2015/11/23 23:37
     */
    public static SpannableString getStrikethroughSpan(Context context, int resourceId, String spanText){
        String resource = context.getString(resourceId);
        SpannableString msp = new SpannableString(context.getString(resourceId, spanText));
        //获取%s的位置
        int index = resource.indexOf("%");
        msp.setSpan(new StrikethroughSpan(), 0, index + spanText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp;
    }

    /**
     * 设置主页title的Span
     *
     * @author zzd
     *
     * created at 2015/11/21 21:48
     */
    public static void setTextSpan(Context context, TextView tv, int strId) {
        SpannableString msp = new SpannableString(context.getString(strId));
        msp.setSpan(new RelativeSizeSpan(0.8f), 6, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.main_gray)), 5, msp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(msp);
    }

    /**
     * add by joyisn
     */
    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static String getCurTimeStr() {
        Calendar cal = Calendar.getInstance();
        String curDate = dateFormater.get().format(cal.getTime());
        return curDate;
    }


    /**
     * add by joyisn
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static boolean checkIfIsEmpty(String s){
        if(null == s || "".equals(s) || "".equals(s.trim())){
            AppContext.showToast("请输入用户名");
            return false;
        }
        return true;
    }

    /**
     * add by joyisn
     * @param userName
     * @return 用户名是否有效
     */
    public static boolean checkUserName(String userName){
        if(null == userName || "".equals(userName) || "".equals(userName.trim())){
            AppContext.showToast("请输入用户名");
            return false;
        }
        return true;
    }

    /**
     * add by joyisn
     * @param firstPwd
     * @param secondPwd
     * @return 比较密码是否有效
     */
    public static boolean checkCouplePwd(String firstPwd, String secondPwd){
        if (null == firstPwd || "".equals(firstPwd) || "".equals(firstPwd.trim()) || null == secondPwd || "".equals(secondPwd) || "".equals(secondPwd.trim())) {
            AppContext.showToast("请输入密码");
            return false;
        } else if (firstPwd.contains(" ")) {
            AppContext.showToast("密码不能包含空格");
            return false;
        } else if (firstPwd.length() < 6 || firstPwd.length() > 20) {
            AppContext.showToast("密码长度为6-20字符");
            return false;
        } else if (!firstPwd.equals(secondPwd.trim())) {
            AppContext.showToast("输入的密码不一致");
            return false;
        }
        return true;
    }

    public static boolean checkSinglePwd(String pwd){
        if (null == pwd || "".equals(pwd) || "".equals(pwd.trim())) {
            AppContext.showToast("请输入密码");
            return false;
        } else if (pwd.contains(" ")) {
            AppContext.showToast("密码不能包含空格");
            return false;
        } else if (pwd.length() < 6 || pwd.length() > 20) {
            AppContext.showToast("密码长度为6-20字符");
            return false;
        }
        return true;
    }

    /**
     * add by joyisn
     * @param phoneNumber
     * @return 手机号是否有效
     */
    public static boolean checkPhoneNumber(String phoneNumber) {
        if (null == phoneNumber || "".equals(phoneNumber) || "".equals(phoneNumber.trim())) {
            AppContext.showToast("请输入手机号码");
            return false;
        } else if (phoneNumber.length() != 11 || !phoneNumber.startsWith("1")) {
            AppContext.showToast("手机号码格式不对");
            return false;
        }
        return true;
    }
}
